package com.puman.beautyfarmcounselor.ui.setting;

import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.andexert.library.RippleView;
import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.util.config.GlobalData;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;


/**
 * A placeholder fragment containing a simple view.
 */
@EFragment(R.layout.setting_main)
public class SettingFragment extends Fragment implements RippleView.OnRippleCompleteListener {

    @ViewById(R.id.base_title)
    TextView topTitle;

    @ViewById(R.id.login_in_out_btn)
    RippleView loginLayout;
    @ViewById(R.id.back_btn)
    ImageView backBtn;


    public SettingFragment() {
    }

    @AfterViews
    void updateView() {
        topTitle.setText("设置");
        loginLayout.setOnRippleCompleteListener(this);
    }

    @Click({R.id.setting_message_layout,
            R.id.setting_feedback_layout, R.id.setting_update_layout,
    R.id.back_btn})
    public void onClick(View view) {
        Fragment fragment = null;
        boolean jump = false;
        switch (view.getId()) {
            case R.id.setting_message_layout:
                //Toast.makeText(MainActivity.getInstace(), "消息中心", Toast.LENGTH_SHORT).show();
                MainActivity.getInstace().switchFragment(this, new MegListFragment_());
                break;
//            case R.id.setting_base_layout:
//                Toast.makeText(MainActivity.getInstace(), "获取密码", Toast.LENGTH_SHORT).show();
//                jump = true;
//                fragment = new ResetFragment_();
//                break;
            case R.id.setting_feedback_layout:
                //  Toast.makeText(MainActivity.getInstace(), "意见反馈", Toast.LENGTH_SHORT).show();
                MainActivity.getInstace().switchFragment(this, new SettingMsgFragment_());
                return;
            case R.id.setting_update_layout:
//                Toast.makeText(MainActivity.getInstace(), "版本更新", Toast.LENGTH_SHORT).show();
//                UmengUpdateAgent.forceUpdate(MainActivity.getInstace());
                updateClick();
                break;
            case R.id.back_btn:
                MainActivity.getInstace().getSupportFragmentManager().popBackStack();
                break;
        }
        if (jump) {
            MainActivity.getInstace().switchFragment(this, fragment);
        }

    }

    /**
     * 更新操作
     */
    private void updateClick() {
        UmengUpdateAgent.forceUpdate(MainActivity.getInstace());
        UmengUpdateAgent.setDeltaUpdate(true);//设置增量更新
        UmengUpdateAgent.setUpdateOnlyWifi(false);
        UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
            @Override
            public void onUpdateReturned(int updateStatus, UpdateResponse updateResponse) {
                switch (updateStatus) {
                    case UpdateStatus.Yes: // has update
                        UmengUpdateAgent.showUpdateDialog(MainActivity.getInstace(), updateResponse);
                        break;
                    case UpdateStatus.No: // has no update
                        Toast.makeText(MainActivity.getInstace(), "已经是最新版本", Toast.LENGTH_SHORT).show();
                        break;
                    case UpdateStatus.Timeout: // time out
                        Toast.makeText(MainActivity.getInstace(), "网络不稳定，请稍后更新 ....", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    public void onComplete(RippleView rippleView) {
        Fragment fragment = null;
        boolean jump = false;
        switch (rippleView.getId()) {
            case R.id.login_in_out_btn:
//                Toast.makeText(MainActivity.getInstace(), "登录登出", Toast.LENGTH_SHORT).show();
                jump = true;
                fragment = new LoginFragment_();
                break;
        }
        if (jump) {
//            MainActivity.getInstace().switchFragment(this, fragment);
            GlobalData.counselor = null;
            MainActivity.getInstace().getSupportFragmentManager()
                    .beginTransaction()
                    .hide(this).replace(R.id.content_none_bottom, fragment).commit();
        }
    }
}
